# Podfile for AutoDev iOS App

platform :ios, '14.0'

# Disable input/output paths for CocoaPods
install! 'cocoapods', :disable_input_output_paths => true

target 'AutoDevApp' do
  use_frameworks!

  # AutoDev Framework - 使用本地编译的 framework
  # 注意：只需要链接 AutoDevUI，因为它已经 export 了 AutoDevCore
  # 同时链接两个会导致重复符号错误
  pod 'AutoDevUI', :path => '../mpp-ui'
end

# Post install hook - 配置编译设置
post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      # 禁用 Bitcode (Kotlin/Native 不支持)
      config.build_settings['ENABLE_BITCODE'] = 'NO'

      # 设置最低部署目标
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '14.0'

      # 优化设置
      config.build_settings['SWIFT_OPTIMIZATION_LEVEL'] = '-Onone' if config.name == 'Debug'
    end
  end

  # 配置主应用 target 的链接器标志和框架搜索路径
  installer.pods_project.targets.each do |target|
    if target.name == 'Pods-AutoDevApp'
      target.build_configurations.each do |config|
        # 配置对应 Debug/Release 的目录名
        build_config = config.name.downcase.include?('debug') ? 'debug' : 'release'

        # 为不同 SDK 设置搜索路径（设备与模拟器）
        framework_search_sim = [
          '$(inherited)',
          "\"${PODS_ROOT}/../../mpp-ui/build/bin/iosSimulatorArm64/#{build_config}Framework\"",
          "\"${PODS_ROOT}/../../mpp-ui/build/bin/iosX64/#{build_config}Framework\""
        ]
        framework_search_dev = [
          '$(inherited)',
          "\"${PODS_ROOT}/../../mpp-ui/build/bin/iosArm64/#{build_config}Framework\""
        ]

        config.build_settings['FRAMEWORK_SEARCH_PATHS[sdk=iphonesimulator*]'] = framework_search_sim
        config.build_settings['FRAMEWORK_SEARCH_PATHS[sdk=iphoneos*]'] = framework_search_dev

        # 添加链接器标志（仅需 AutoDevUI，已 export AutoDevCore）
        ldflags = [
          '$(inherited)',
          '-ObjC',
          '-lc++',
          '-framework AutoDevUI',
          '-lsqlite3'
        ]
        config.build_settings['OTHER_LDFLAGS'] = ldflags
      end
    end
  end

  # 直接修改主应用项目的构建设置，确保 sqlite3 被链接
  app_project = Xcodeproj::Project.open('AutoDevApp.xcodeproj')
  app_project.targets.each do |target|
    if target.name == 'AutoDevApp'
      target.build_configurations.each do |config|
        # 配置 Debug/Release 名称
        build_config = config.name.downcase.include?('debug') ? 'debug' : 'release'

        # 添加 AutoDevUI 框架搜索路径（设备与模拟器）
        framework_search_sim = [
          '$(inherited)',
          "\"${SRCROOT}/../mpp-ui/build/bin/iosSimulatorArm64/#{build_config}Framework\"",
          "\"${SRCROOT}/../mpp-ui/build/bin/iosX64/#{build_config}Framework\""
        ]
        framework_search_dev = [
          '$(inherited)',
          "\"${SRCROOT}/../mpp-ui/build/bin/iosArm64/#{build_config}Framework\""
        ]

        config.build_settings['FRAMEWORK_SEARCH_PATHS[sdk=iphonesimulator*]'] = framework_search_sim
        config.build_settings['FRAMEWORK_SEARCH_PATHS[sdk=iphoneos*]'] = framework_search_dev

        # 添加链接器标志，确保链接 AutoDevUI 和 sqlite3
        flags = config.build_settings['OTHER_LDFLAGS'] || ['$(inherited)']
        flags = [flags] unless flags.is_a?(Array)
        ['-ObjC','-lc++','-framework','AutoDevUI','-lsqlite3'].each do |f|
          flags << f unless flags.include?(f)
        end
        config.build_settings['OTHER_LDFLAGS'] = flags
      end
    end
  end
  app_project.save
end

